﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QN_DiemSV.Classes
{
    class connect
    {
        string strConn = "Data Source=.;Initial Catalog=quanlisinhvien;Integrated Security=True";
        SqlConnection conn;
        SqlConnection MoKetNoi()
        {
            conn = new SqlConnection(strConn);
            return conn;
        }
        public DataTable excuteSelect(string sql)
        {
            SqlConnection sqlconn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da;
            DataTable dt = new DataTable();
            cmd.Connection = sqlconn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            return dt;
        }
        public void excuteNonquery(string sql)
        {
            SqlConnection sqlconn = MoKetNoi();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlconn;
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = sql;
            sqlconn.Open();
            cmd.ExecuteNonQuery();
        }
        public void excuteNoquery(string sql, int Ma)
        {
            SqlConnection sqlconn = MoKetNoi();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlconn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql + Ma;
            cmd.ExecuteNonQuery();
        }
        public void EXE_PROC_PARAMS(string name_proc, params object[] pars)
        {
            SqlCommand cmd = null;
            SqlConnection con = new SqlConnection(strConn);
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = name_proc;
            for (int i = 0; i < pars.Length; i += 2)
            {
                SqlParameter par = new SqlParameter(pars[i].ToString(), pars[i + 1]);
                cmd.Parameters.Add(par);
            }
            SqlDataReader ds;
            con.Open();
            ds = cmd.ExecuteReader();
            cmd.Dispose();
            con.Close();
        }
    }
}
